﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using DataObjects.Interface;
using DataObjects;
using ImageService.DataTransferObjectMapper;
using System.ServiceModel.Web;
using ImageService.DataTransferObjects;
using BusinessObjects;
using ImageService.Message;

namespace ImageService.ServiceImplementations
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class ImageActionService //: IImageActionService//
    {
        private static readonly IRestaurantMenuDao _restaurantMenuDao = DataAccess.RestaurantMenuDao;
        private static readonly IRestaurantMenuCategoryDao _restaurantMenuCategoryDao = DataAccess.RestaurantMenuCategoryDao;

        [WebGet(UriTemplate = "GetRestaurantMenu/{menuId}", BodyStyle = WebMessageBodyStyle.Bare)]
        public Stream GetRestaurantMenu(string menuId)
        {
            RestaurantMenuDto restaurantMenuDto = new RestaurantMenuDto();

            if (menuId != null && menuId.Length > 0)
            {
                var restaurantMenu = _restaurantMenuDao.GetRestaurantMenu(menuId);
                restaurantMenuDto = Mapper.ToDataTransferObject(restaurantMenu);
            }

            if (restaurantMenuDto.MenuImage != null)
            {
                MemoryStream ms = new MemoryStream(restaurantMenuDto.MenuImage);
                return ms;
            }
            else
                return null;
        }

        [WebInvoke(UriTemplate = "SetRestaurantMenus/{action}/{menuId}/{menuCode}/{menuName}/{description}/{unitId}/{salesPrice}/{isActive}/{isNorm}/{restaurantId}/{menuCategoryId}", ResponseFormat = WebMessageFormat.Json, Method = "POST")]
        public RestaurantMenuResponse SetRestaurantMenus(string action, string menuId, string menuCode, string menuName, string description, string unitId, string salesPrice, string isActive, string isNorm, string restaurantId, string menuCategoryId, Stream menuImage)
        {
            RestaurantMenuResponse response = new RestaurantMenuResponse();
            string _menuid = null;
            string _description = null;

            if (description == null || description.Contains("null")) { _description = null; }
            else { _description = description; }

            if (menuId == null || menuId.Contains("null")) { _menuid = null; }
            else { _menuid = menuId; }

            var restaurantMenuDto = new RestaurantMenuDto
            {
                MenuID = _menuid,
                MenuCode = menuCode,
                MenuName = menuName,
                Description = _description,
                UnitID = unitId,
                SalesPrice = decimal.Parse(salesPrice.ToString()),
                MenuImage = Utils.ImageExtension.FromStreamToByte(menuImage),
                IsActive = bool.Parse(isActive.ToString()),
                IsNorm = bool.Parse(isNorm.ToString()),
                RestaurantID = restaurantId,
                MenuCategoryID = menuCategoryId,
            };

            var restaurantMenu = Mapper.FromDataTransferObject(restaurantMenuDto);
            response.ErrorCode = 0;
            if (action != "Delete")
            {
                if (!restaurantMenu.Validate())
                {
                    foreach (string error in restaurantMenu.ValidationErrors)
                        response.Message += error + Environment.NewLine;

                    response.ErrorCode = 1;
                    return response;
                }
            }
            try
            {
                if (action == "Create") { _restaurantMenuDao.InsertRestaurantMenu(restaurantMenu); }
                else if (action == "Update") { _restaurantMenuDao.UpdateRestaurantMenu(restaurantMenu); }
                else
                {
                    var _restaurantMenu = _restaurantMenuDao.GetRestaurantMenu(menuId);
                    _restaurantMenuDao.DeleteRestaurantMenu(_restaurantMenu);
                }
            }
            catch (Exception ex)
            {
                response.ErrorCode = 1;
                response.Message = ex.Message;
                return response; ;
            }

            return response;
        }
    }
}
